package vn.com.aptech.msh.controller;

import java.io.Serializable;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;

import javax.enterprise.context.RequestScoped;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.ValidatorException;
import javax.inject.Inject;
import javax.inject.Named;
import javax.validation.constraints.NotNull;
import vn.com.aptech.msh.business.IUserBean;
import vn.com.aptech.msh.entities.User;
import vn.com.aptech.msh.utils.MD5Utility;
import vn.com.aptech.msh.utils.NavigationHelper;
import vn.com.aptech.msh.utils.ResourceBundleHelper;

/**
 *
 * @author trietto
 */
@Named("changePasswordController")
@RequestScoped
public class ChangePasswordController implements Serializable {

    @Inject
    LoginController loginController;
    @Inject
    IUserBean userBusiness;
    @Inject
    @Named("currentUser")
    User currentUser;
    @NotNull
    private String currentPassword;
    @NotNull
    private String newPassword;
    @NotNull
    private String confirmNewPassword;

    @PostConstruct
    public void init() {
        currentPassword = "";
        newPassword = "";
        confirmNewPassword = "";
    }

    public String changePassword() {
        try {
            User newUser = userBusiness.changePassword(currentUser, newPassword);
            loginController.setCurrentUser(newUser);
            return NavigationHelper.toSuccessPage("changepwd");
        } catch (Exception ex) {
            Logger.getLogger(ChangePasswordController.class.getName()).log(Level.SEVERE, null, ex);
            FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, ResourceBundleHelper.get("changepwd.failed"), "");
            FacesContext.getCurrentInstance().addMessage("frmChangePwd:changePwdBtn", msg);
            return null;
        }
    }

    public void validateCurrentPassword(FacesContext context, UIComponent toValidate, Object value)
            throws ValidatorException {
        String tCurPassword = (String) value;
        if (!MD5Utility.compare(tCurPassword, currentUser.getPassword())) {
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, ResourceBundleHelper.get("validator.changepwd.wrongCurPwd"), "");
            throw new ValidatorException(message);
        }
    }

    public String getConfirmNewPassword() {
        return confirmNewPassword;
    }

    public void setConfirmNewPassword(String confirmNewPassword) {
        this.confirmNewPassword = confirmNewPassword;
    }

    public String getCurrentPassword() {
        return currentPassword;
    }

    public void setCurrentPassword(String currentPassword) {
        this.currentPassword = currentPassword;
    }

    public String getNewPassword() {
        return newPassword;
    }

    public void setNewPassword(String newPassword) {
        this.newPassword = newPassword;
    }
}
